﻿/*  ------------------------------------------------------------------------
	NQ Common Services Library
	
	Homepage: http://www.awzhome.de/
	------------------------------------------------------------------------
	
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
   
	The Original Code is code of NQ Common Services Library.

	The Initial Developer of the Original Code is Andreas Weizel.
	Portions created by the Initial Developer are
	Copyright (C) 2011 Andreas Weizel. All Rights Reserved.
	
	Contributor(s): (none)
	
	Alternatively, the contents of this file may be used under the terms
	of the GNU General Public License Version 2.0 or later (the "GPL"),
	or the GNU Lesser General Public License Version 2.1 or later (the
	"LGPL"), in which case the provisions of the GPL or the LGPL are
	applicable instead of those above. If you wish to allow use of your
	version of this file only under the terms of the GPL or the LGPL and
	not to allow others to use your version of this file under the MPL,
	indicate your decision by deleting the provisions above and replace
	them with the notice and other provisions required by the GPL or the
	LGPL. If you do not delete the provisions above, a recipient may use
	your version of this file under the terms of any of the MPL, the GPL
	or the LGPL.
	------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace AWZhome.NQ.CommonServices
{
	/// <summary>
	/// Extension methods for basic type conversion of <see cref="AWZhome.NQ.CommonServices.ConfigData"/>
	/// objects.
	/// </summary>
	public static class ConfigDataExtensions
	{
		private const string GeneralCulture = "en-GB";

		/// <summary>
		/// Converts a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object to a string.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <returns>Config data as string or <c>null</c>, if object is <c>null</c>.</returns>
		public static string AsString(this ConfigData configData)
		{
			return (configData != null) ? configData.RawData : null;
		}

		/// <summary>
		/// Converts a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object to a string or uses a default value.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <param name="defaultValue">Default value.</param>
		/// <returns>Config data as string or the given default value, if object is <c>null</c>.</returns>
		public static string AsString(this ConfigData configData, string defaultValue)
		{
			return (configData != null) ? configData.RawData : defaultValue;
		}

		/// <summary>
		/// Writes a string value to a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <param name="data">Data to write.</param>
		/// <returns>The converted config object.</returns>
		public static ConfigData FromString(this ConfigData configData, string data)
		{
			if (configData != null)
			{
				configData.RawData = data;
				return configData;
			}
			else
			{
				throw new NullReferenceException("ConfigData is null.");
			}
		}

		/// <summary>
		/// Converts a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object to a <see cref="System.Int32"/>.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <returns>Converted config data or <c>null</c>, if object is <c>null</c>.</returns>
		public static System.Int32? AsInt32(this ConfigData configData)
		{
			if (configData != null)
			{
				Int32 temp;
				if (Int32.TryParse(configData.RawData, out temp))
				{
					return temp;
				}
				else
				{
					return null;
				}
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// Converts a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object to a <see cref="System.Int32"/>
		/// or uses a default value.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <param name="defaultValue">Default value.</param>
		/// <returns>Converted config data or the given default value, if object is <c>null</c>.</returns>
		public static System.Int32 AsInt32(this ConfigData configData, System.Int32 defaultValue)
		{
			if (configData != null)
			{
				Int32 temp;
				if (Int32.TryParse(configData.RawData, out temp))
				{
					return temp;
				}
				else
				{
					return defaultValue;
				}
			}
			else
			{
				return defaultValue;
			}
		}

		/// <summary>
		/// Writes a <see cref="System.Int32"/> value to a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <param name="data">Data to write.</param>
		/// <returns>The converted config object.</returns>
		public static ConfigData FromInt32(this ConfigData configData, System.Int32 data)
		{
			if (configData != null)
			{
				configData.RawData = data.ToString();
				return configData;
			}
			else
			{
				throw new NullReferenceException("ConfigData is null.");
			}
		}

		/// <summary>
		/// Converts a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object to a <see cref="System.Int64"/>.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <returns>Converted config data or <c>null</c>, if object is <c>null</c>.</returns>
		public static System.Int64? AsInt64(this ConfigData configData)
		{
			if (configData != null)
			{
				Int64 temp;
				if (Int64.TryParse(configData.RawData, out temp))
				{
					return temp;
				}
				else
				{
					return null;
				}
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// Converts a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object to a <see cref="System.Int64"/>
		/// or uses a default value.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <param name="defaultValue">Default value.</param>
		/// <returns>Converted config data or the given default value, if object is <c>null</c>.</returns>
		public static System.Int64 AsInt64(this ConfigData configData, System.Int64 defaultValue)
		{
			if (configData != null)
			{
				Int64 temp;
				if (Int64.TryParse(configData.RawData, out temp))
				{
					return temp;
				}
				else
				{
					return defaultValue;
				}
			}
			else
			{
				return defaultValue;
			}
		}

		/// <summary>
		/// Writes a <see cref="System.Int64"/> value to a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <param name="data">Data to write.</param>
		/// <returns>The converted config object.</returns>
		public static ConfigData FromInt64(this ConfigData configData, System.Int64 data)
		{
			if (configData != null)
			{
				configData.RawData = data.ToString();
				return configData;
			}
			else
			{
				throw new NullReferenceException("ConfigData is null.");
			}
		}

		/// <summary>
		/// Converts a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object to a <see cref="System.Double"/>.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <returns>Converted config data or <c>null</c>, if object is <c>null</c>.</returns>
		public static double? AsDouble(this ConfigData configData)
		{
			if (configData != null)
			{
				double temp;
				if (double.TryParse(configData.RawData, out temp))
				{
					return temp;
				}
				else
				{
					return null;
				}
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// Converts a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object to a <see cref="System.Double"/>
		/// or uses a default value.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <param name="defaultValue">Default value.</param>
		/// <returns>Converted config data or the given default value, if object is <c>null</c>.</returns>
		public static double AsDouble(this ConfigData configData, double defaultValue)
		{
			if (configData != null)
			{
				double temp;
				// Always use the en-GB culture to convert the Double value
				if (Double.TryParse(configData.RawData, NumberStyles.Float, CultureInfo.GetCultureInfo(GeneralCulture).NumberFormat, out temp))
				{
					return temp;
				}
				else
				{
					return defaultValue;
				}
			}
			else
			{
				return defaultValue;
			}
		}

		/// <summary>
		/// Writes a <see cref="System.Double"/> value to a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <param name="data">Data to write.</param>
		/// <returns>The converted config object.</returns>
		public static ConfigData FromDouble(this ConfigData configData, double data)
		{
			if (configData != null)
			{
				// Always use the en-GB culture to convert the Double value
				configData.RawData = data.ToString(CultureInfo.GetCultureInfo(GeneralCulture).NumberFormat);
				return configData;
			}
			else
			{
				throw new NullReferenceException("ConfigData is null.");
			}
		}

		/// <summary>
		/// Converts a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object to a <see cref="System.Boolean"/>.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <returns>Converted config data or <c>null</c>, if object is <c>null</c>.</returns>
		public static bool? AsBoolean(this ConfigData configData)
		{
			if (configData != null)
			{
				return configData.RawData == "true";
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// Converts a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object to a <see cref="System.Boolean"/>
		/// or uses a default value.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <param name="defaultValue">Default value.</param>
		/// <returns>Converted config data or the given default value, if object is <c>null</c>.</returns>
		public static bool AsBoolean(this ConfigData configData, bool defaultValue)
		{
			if (configData != null)
			{
				if (configData != null)
				{
					return configData.RawData == "true";
				}
				else
				{
					return defaultValue;
				}
			}
			else
			{
				return defaultValue;
			}
		}

		/// <summary>
		/// Writes a <see cref="System.Boolean"/> value to a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <param name="data">Data to write.</param>
		/// <returns>The converted config object.</returns>
		public static ConfigData FromBoolean(this ConfigData configData, bool data)
		{
			if (configData != null)
			{
				configData.RawData = (data) ? "true" : "false";
				return configData;
			}
			else
			{
				throw new NullReferenceException("ConfigData is null.");
			}
		}

		/// <summary>
		/// Converts a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object to a 
		/// <see cref="System.Collections.Generic.IEnumerable#string"/>.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <returns>Converted config data or <c>null</c>, if object is <c>null</c>.</returns>
		public static IEnumerable<string> AsStringList(this ConfigData configData)
		{
			if (configData != null)
			{
				return configData.RawData.Split('|');
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// Converts a <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object to a
		/// <see cref="System.Collections.Generic.IEnumerable#string"/> or uses a default value.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <param name="defaultValue">Default value.</param>
		/// <returns>Converted config data or the given default value, if object is <c>null</c>.</returns>
		public static IEnumerable<string> AsStringList(this ConfigData configData, IEnumerable<string> defaultValue)
		{
			if (configData != null)
			{
				return configData.RawData.Split('|');
			}
			else
			{
				return defaultValue;
			}
		}

		/// <summary>
		/// Writes a <see cref="System.Collections.Generic.IEnumerable#string"/> to a
		/// <see cref="AWZhome.NQ.CommonServices.ConfigData"/> object.
		/// </summary>
		/// <param name="configData">Config data object.</param>
		/// <param name="data">Data to write.</param>
		/// <returns>The converted config object.</returns>
		public static ConfigData FromStringList(this ConfigData configData, IEnumerable<string> data)
		{
			if (configData != null)
			{
				bool isFirst = true;
				configData.RawData = "";
				foreach (string stringElement in data)
				{
					if (!isFirst)
					{
						configData.RawData += "|";
					}
					configData.RawData += stringElement;
					isFirst = false;
				}

				return configData;
			}
			else
			{
				throw new NullReferenceException("ConfigData is null.");
			}
		}
	}
}
